package com.soft.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@WebServlet(value = "/HttpsProcess2", asyncSupported = false)
public class UploadServlet extends HttpServlet {
	 private static String USER = "sunyard";
	  private static String PASSWORD = "admin123";
	private static String SUCCESS = "0000";
	private static String UPLOAD_ERR = "0001";
	private static String LOGON_USER_PASSWORD_ERR = "0002";
	private static String EXCEPTION_ERR = "0003";

	private static String tempDir = "d:\\test\\";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("aaaaaaaaaaaaaaaaaa");
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(4096);
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setSizeMax(104857600L);
		try {
			List<FileItem> list = upload.parseRequest(req);
			Iterator<FileItem> iter = list.iterator();
			File zipTempfile = null;
			String name = "";
			 String[] arrString = null;
			String batchTemPath = tempDir + File.separator + "scan"
					+ File.separator + "aa";
			File tempFile = new File(batchTemPath);
			if (!tempFile.exists()) {
				tempFile.mkdirs();
			}
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				if (item.isFormField()) {
					String fieldValue = new String(item.getString().getBytes(
							"ISO8859_1"), "utf-8");

					System.out.println("fieldValue=" + fieldValue);

					Hex hex = new Hex();
					byte[] hFieldValue = (byte[]) hex.decode(fieldValue);
					fieldValue = new String(hFieldValue);
					System.out.println("HTTP:" + fieldValue);

					Base64 base64 = new Base64();
					byte[] dFieldValue = base64.decode(hFieldValue);
					fieldValue = new String(dFieldValue);

					arrString = fieldValue.split(",");
					String user = "";
					String password = "";
					if (arrString.length > 0) {
						if ((arrString[3] != null) || (arrString[4] != null)) {
							user = arrString[3];
							password = arrString[4];
						} else {
							throw new Exception(
									"filed value is err ,pls check it then try again");
						}
					}
					if ((!user.equalsIgnoreCase(USER))
							|| (!password.equalsIgnoreCase(PASSWORD))) {
						resp.setHeader("retcode", LOGON_USER_PASSWORD_ERR);
					}

				} else {
					name = item.getName();
					System.out.println("item.getName=" + name);

					zipTempfile = new File(batchTemPath + File.separator + name);
					item.write(zipTempfile);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			resp.setHeader("retcode", EXCEPTION_ERR);
		}
		
		resp.setHeader("retcode", SUCCESS);
	}
}
